/* 
 * File:   Lesson10.cpp
 * Author: jose
 * 
 * Created on March 29, 2012, 6:50 PM
 */

#include "Lesson10.h"
#include "SdlUtils.h"

Lesson10::Lesson10() {
}

Lesson10::Lesson10(const Lesson10& orig) {
}

Lesson10::~Lesson10() {
}

bool Lesson10::init() {
    //Initialize all SDL subsystems
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    //If there was an error in setting up the screen
    if (screen == NULL) {
        return false;
    }

    //Initialize SDL_ttf
    if (TTF_Init() == -1) {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption("TTF Test", NULL);

    //If everything initialized fine
    return true;
}

bool Lesson10::loadFiles() {
    //Load the background image
    background = SdlUtils::loadImage("blackbackground.png");

    //Open the font
    font = TTF_OpenFont("lazy.ttf", 28);

    //If there was a problem in loading the background
    if (background == NULL) {
        return false;
    }

    //If there was an error in loading the font
    if (font == NULL) {
        return false;
    }

    //If everything loaded fine
    return true;
}

void Lesson10::cleanUp() {
    //Free the surfaces
    SDL_FreeSurface(background);
    SDL_FreeSurface(upMessage);
    SDL_FreeSurface(downMessage);
    SDL_FreeSurface(rightMessage);
    SDL_FreeSurface(leftMessage);
    

    //Close the font that was used
    TTF_CloseFont(font);

    //Quit SDL_ttf
    TTF_Quit();

    //Quit SDL
    SDL_Quit();
}

int Lesson10::Run() {

    //The event structure
    SDL_Event event;

    //The color of the font
    SDL_Color textColor = {255, 255, 255};

    //Quit flag
    bool quit = false;

    //Initialize
    if (init() == false) {
        return 1;
    }

    //Load the files
    if (loadFiles() == false) {
        return 1;
    }

    //Generate the message surfaces 
    upMessage = TTF_RenderText_Solid(font, "Up was pressed.", textColor);
    downMessage = TTF_RenderText_Solid(font, "Down was pressed.", textColor);
    leftMessage = TTF_RenderText_Solid(font, "Left was pressed", textColor);
    rightMessage = TTF_RenderText_Solid(font, "Right was pressed", textColor);

    if ((upMessage == NULL) || (downMessage == NULL) ||
            (leftMessage == NULL) || (rightMessage == NULL)) {
        return 1;
    }

    SdlUtils::applySurface(0, 0, background, screen);
    SdlUtils::applySurface(screen->w / 4, screen->h / 2, message, screen);


    SdlUtils::applySurface(0, 0, background, screen);

    while (!quit) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_KEYDOWN) {
                switch (event.key.keysym.sym) {
                    case SDLK_ESCAPE: quit = true;
                        break;
                    default:;
                }
            } else if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
        
          //Get the keystates
        Uint8 *keystates = SDL_GetKeyState( NULL );

        SdlUtils::applySurface(0, 0, background, screen);
        
        //If up is pressed
        if( keystates[ SDLK_UP ] )
        {
            SdlUtils::applySurface( ( SCREEN_WIDTH - upMessage->w ) / 2, ( SCREEN_HEIGHT / 2 - upMessage->h ) / 2, upMessage, screen );
        }

        //If down is pressed
        if( keystates[ SDLK_DOWN ] )
        {
            SdlUtils::applySurface( ( SCREEN_WIDTH - downMessage->w ) / 2, ( SCREEN_HEIGHT / 2 - downMessage->h ) / 2 + ( SCREEN_HEIGHT / 2 ), downMessage, screen );
        }

        //If left is pressed
        if( keystates[ SDLK_LEFT ] )
        {
            SdlUtils::applySurface( ( SCREEN_WIDTH / 2 - leftMessage->w ) / 2, ( SCREEN_HEIGHT - leftMessage->h ) / 2, leftMessage, screen );
        }

        //If right is pressed
        if( keystates[ SDLK_RIGHT ] )
        {
            SdlUtils::applySurface( ( SCREEN_WIDTH / 2 - rightMessage->w ) / 2 + ( SCREEN_WIDTH / 2 ), ( SCREEN_HEIGHT - rightMessage->h ) / 2, rightMessage, screen );
        }
        
        if (SDL_Flip(screen) == -1) {
            return 1;
        }
    }

    cleanUp();
}